<script setup>
import { onMounted, ref } from 'vue'
import dial from './dial.vue'
// 假设后续会创建API文件
// import { getAllApi, deleteApi } from '@/api/stocktaking/loss'

const formData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const size = ref('default')
const background = ref(true)
const disabled = ref(false)

// 搜索表单
const form = ref({
  lossCategory: '',
  stocktakingInfo: ''
})

// 损失类型选项
const lossCategoryOptions = [
  { value: 1, label: '破损' },
  { value: 2, label: '丢失' }
]

// 弹窗引用
const diaRef = ref(null)

// 分页相关方法
const handleSizeChange = (val) => {
  pageSize.value = val
  getAll()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  getAll()
}

// 获取数据
const getAll = async () => {
  // 模拟数据获取
  // let res = await getAllApi(
  //   form.value.lossCategory,
  //   form.value.stocktakingInfo,
  //   currentPage.value,
  //   pageSize.value
  // )
  // total.value = res.total
  // formData.value = res.rows
  
  // 临时模拟数据
  formData.value = [
    {
      id: 1,
      lossCount: 2,
      lossCategory: 1,
      stocktakingInfo: 1,
      empId: 101,
      createTime: '2023-05-01 10:30:00',
      createBy: 'admin'
    },
    {
      id: 2,
      lossCount: 1,
      lossCategory: 2,
      stocktakingInfo: 2,
      empId: 102,
      createTime: '2023-05-02 15:00:00',
      createBy: 'manager'
    }
  ]
  total.value = 2
}

// 搜索
const search = () => {
  currentPage.value = 1
  getAll()
}

// 重置搜索
const resetSearch = () => {
  form.value = {
    lossCategory: '',
    stocktakingInfo: ''
  }
  search()
}

// 添加
const handleAdd = () => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    diaRef.value.resetForm()
  }
}

// 编辑
const handleEdit = (row) => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    // 模拟数据回显
    diaRef.value.dialogFormData = { ...row }
  }
}

// 删除
const handleDelete = (id) => {
  ElMessageBox.confirm('确定要删除这条记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    // await deleteApi(id)
    ElMessage.success('删除成功')
    getAll()
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 查看详情
const handleViewDetails = (row) => {
  if (diaRef.value) {
    diaRef.value.dialogVisible = true
    diaRef.value.dialogFormData = { ...row }
    diaRef.value.isViewMode = true
  }
}

onMounted(() => {
  getAll()
})
</script>

<template>
  <dial ref="diaRef" @refresh="getAll" />
  
  <!-- 搜索表单 -->
  <el-form :model="form" :inline="true" class="search-form">
    <el-form-item label="损失类型">
      <el-select v-model="form.lossCategory" placeholder="请选择损失类型" clearable>
        <el-option
          v-for="item in lossCategoryOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="盘点信息ID">
      <el-input v-model="form.stocktakingInfo" placeholder="请输入盘点信息ID" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </el-form-item>
  </el-form>

  <!-- 操作按钮 -->
  <div class="toolbar">
    <el-button type="primary" @click="handleAdd">新增报损</el-button>
  </div>

  <!-- 数据表格 -->
  <el-table :data="formData" style="width: 100%" border>
    <el-table-column prop="id" label="主键ID" width="100" />
    <el-table-column prop="lossCount" label="损失数量" width="100" />
    <el-table-column prop="lossCategory" label="损失类型" width="120">
      <template #default="{ row }">
        <el-tag :type="row.lossCategory === 1 ? 'warning' : 'danger'">
          {{ row.lossCategory === 1 ? '破损' : '丢失' }}
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="stocktakingInfo" label="盘点信息ID" width="120" />
    <el-table-column prop="empId" label="员工ID" width="100" />
    <el-table-column prop="createTime" label="创建时间" width="180" />
    <el-table-column prop="createBy" label="创建人" width="120" />
    <el-table-column fixed="right" label="操作" width="200">
      <template #default="{ row }">
        <el-button type="primary" size="small" @click="handleViewDetails(row)">查看</el-button>
        <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDelete(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <div class="pagination-container">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 50]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<style scoped>
.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.toolbar {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>